import {getUser,getUserAns} from '@/api/user'
interface IState {
    userName: string;
    userEmail: string;
    userAvatar: string;
    userId: string;
}
interface IUserInfo {
    userName: string;
    userEmail: string;
    avatar: string;
    userId: string;
}
const state: IState = {
    userName: '',
    userEmail: '',
    userAvatar: '',
    userId: ''
}
const mutations = {
    setName(state: IState,newName: string){
        state.userName = newName
    },
    setEmail(state: IState,newEmail: string){
        state.userEmail = newEmail
    },
    setAvatar(state: IState,newAvatar: string){
        state.userAvatar = newAvatar
    },
    setId(state: IState,newId: string){
        state.userId = newId
    },
    removeInfo(state: IState){
        state.userName = ''
        state.userEmail = ''
        state.userAvatar = ''
        state.userId = ''
    }
}
const actions = {
    getUserInfo(context: any){
        getUser()
            .then(res => {
                const {code,msg,data} = res as unknown as getUserAns
                if(code === 200){
                    context.commit('setName',(data as IUserInfo).userName)
                    context.commit('setEmail',(data as IUserInfo).userEmail)
                    context.commit('setAvatar',(data as IUserInfo).avatar)
                    context.commit('setId',(data as IUserInfo).userId)
                }else{
                    localStorage.removeItem('token')
                    console.log(msg);
                }
            })
    }
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}